var AdsEdit = function() {

    var handleValidation = function() {
        var formAdd = $('#editForm');
        $('#addModel').click(function(){
         	timeout(this, 1);
         	formAdd.submit();
         });
        var validator = formAdd.validate({
            rules: {
                adsName: {
                    required: true,
                    rangelength: [1, 50]
                },
                orders: {
                    required: true,
                    number: true
                },
                platform :{
                	required: true
                }
            },
            messages: {
                adsName: {
                    required: "请输入广告名称",
                    rangelength: "长度范围为1-50"
                },
                orders: {
                    required: "排序值不能为空",
                    number: "请输入一个数字"
                },
                platform :{
                	required: "请选择发布平台"
                }
            },
            success: function(label, element) {
                var icon = $(element).parent('.input-icon').children('i');
                // set success class to the control group
                $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
                icon.removeClass("fa-warning").addClass("fa-check");
            },

            submitHandler: function(form) {

                var fileUrl = $("#thumbnail").val();
                if (fileUrl == null || fileUrl == "") {
                    bootbox.alert("请添加一张广告图片");
                    return false;
                }
                var url = $("#url").val();
				if ($("#link").val() == "1") {
					if (url == "") {
						bootbox.alert("请添加跳转链接");
						return false;
					}else{
						var start = url.indexOf("http://");
						var starts = url.indexOf("https://");
						if(!(start == 0 || starts == 0)){
							bootbox.alert("跳转链接不符合规则");
							return false;
						}
						$("#productId").val("");
					}
				}
				if($("#link").val() == "0"){
					$("#url").val("");
				}
				var json = $('form#editForm').serializeJSON();
                $.ajax({
                    type: "post",
                    contentType: "application/json; charset=UTF-8",
                    url: ctx + "/noads/update",
                    data: json,
                    success: function(data) {
                        if (data.code == 1) {
                            bootbox.dialog({
                                message: "修改广告成功！",
                                title: "温馨提示：",
                                buttons: {
                                    main: {
                                        label: "确定",
                                        className: "btn btn-primary",
                                        callback: function() {
                                            window.location.href = 'noads/list';
                                        }
                                    }
                                }
                            });
                        } else {
                            bootbox.dialog({
                                message: data.message,
                                title: "温馨提示：",
                                buttons: {
                                    main: {
                                        label: "确定",
                                        className: "btn btn-primary",
                                        callback: function() {
                                            //window.location.reload();
                                        }
                                    }
                                }
                            });
                        }
                    },
                    error: function(msg) {
                        bootbox.alert(msg.message);
                    }
                });
            }
        });
    }

    return {
        init: function() {
            handleValidation();
        }
    };
}();


$("#file").change(
    function() {
        data = new FormData();
        data.append("file", this.files[0]);
        $.ajax({
            data: data,
            type: "POST",
            url: ctx + "/noads/uploadFile",
            cache: false,
            contentType: false,
            processData: false,
            success: function(msg) {
                if (msg.code == 1) {
                    $("#thumbnail").val(msg.data);
                    $("#adsImg").html(
                        '<img src="' +imagePrefix+ msg.data + '" width="256">');
                } else {
                    bootbox.alert("添加图片失败！");
                }
            },
            error: function(msg) {
                bootbox.alert(msg.message);
            }
        });
    });